package com.example.xunai.dto.request.system;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TokenUsageQueryRequest {
    @NotNull(message = "用户ID不能为空")
    private Long userId;

    @NotNull(message = "开始日期不能为空")
    private LocalDate startDate;

    @NotNull(message = "结束日期不能为空")
    private LocalDate endDate;

    private String modelType;

    private Integer limit = 100;

    // 静态工厂方法
    public static TokenUsageQueryRequest of(Long userId, LocalDate startDate, LocalDate endDate) {
        return new TokenUsageQueryRequest(userId, startDate, endDate, null, 100);
    }

    public static TokenUsageQueryRequest of(Long userId, LocalDate startDate, LocalDate endDate, String modelType) {
        return new TokenUsageQueryRequest(userId, startDate, endDate, modelType, 100);
    }

    public static TokenUsageQueryRequest of(Long userId, LocalDate startDate, LocalDate endDate, String modelType, Integer limit) {
        return new TokenUsageQueryRequest(userId, startDate, endDate, modelType, limit);
    }

    public boolean isValid() {
        return userId != null && startDate != null && endDate != null && !startDate.isAfter(endDate);
    }

    public String toCacheKey() {
        if (modelType != null && !modelType.trim().isEmpty()) {
            return String.format("token_stats:%d:%s:%s:%s",
                    userId, startDate, endDate, modelType);
        }
        return String.format("token_stats:%d:%s:%s",
                userId, startDate, endDate);
    }

    /**
     * 获取日期范围描述
     */
    public String getDateRangeDescription() {
        if (startDate.equals(endDate)) {
            return startDate.toString();
        }
        return startDate + " 至 " + endDate;
    }

    /**
     * 检查是否包含特定模型
     */
    public boolean hasModelType() {
        return modelType != null && !modelType.trim().isEmpty();
    }

    /**
     * 获取天数范围
     */
    public long getDaysRange() {
        return java.time.temporal.ChronoUnit.DAYS.between(startDate, endDate) + 1;
    }
}